<?php

class edit_license
{
	var $mess;

	function edit_license()
	{
		if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['itemID']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['itemID']))
		{
			$this->doEdit();
		}
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if(isset($_POST['editItem']) && isset($_POST['itemID']))
		{
			$this->renderEdit();
		}
		else if(isset($_POST['removeItem']) && isset($_POST['itemID']))
		{
			$this->mess->render();
			$this->renderAdd();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['itemID']))
		{
			$this->mess->render();
			$this->renderEdit();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['itemID']))
		{
			$this->mess->render();
			$this->renderEdit();
		}
		else
		{
			$this->renderAdd();
		}
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select l_name from ". LICENSES_TABLE . " Where l_id = " . $_POST['itemID'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qDel = "delete from ". LICENSES_TABLE . " Where l_id = " . $_POST['itemID'];
			$result = $db->sql_query($qDel);
			$this->mess = new addMessage(str_replace('{entry}', $row['l_name'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". LICENSES_TABLE . " (l_name, l_minRaces, l_maxPoints, l_pointDuration, l_default, l_order, l_about) values('" . 
					$_POST['licenseName'] . "', " . $_POST['minRaces'] . ", " . $_POST['maxPoints'] . ", " . $_POST['pointDuration'] . ", " .
					$_POST['default'] . ", " . $_POST['order'] . ", '" . $_POST['about'] . "')" ;

			$result = $db->sql_query($qUpd);
			$this->mess = new addMessage(str_replace('{entry}', $_POST['licenseName'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". LICENSES_TABLE . " set l_name = '" . $_POST['licenseName'] .
											     "', l_minRaces = " . $_POST['minRaces'] .
												 ", l_maxPoints = " . $_POST['maxPoints'] .
												 ", l_pointDuration = " . $_POST['pointDuration'] .
												 ", l_default = " . $_POST['default'] .
												 ", l_order = " . $_POST['order'] .
												 ", l_about = '" . $_POST['about'] .
												 "' WHERE l_id = " . $_POST['itemID'] ;
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['licenseName'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['itemID']);
		}
		
		$valid = $valid && is_numeric($_POST['minRaces']) && is_numeric($_POST['maxPoints']) && is_numeric($_POST['pointDuration']) && is_numeric($_POST['default']) && is_numeric($_POST['order']);
		return $valid;
					  
	}
	
	function renderEdit()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$q = "Select * From ". LICENSES_TABLE . " Where l_id = " . $_POST['itemID'];
		$result = $db->sql_query($q);
		if($row = $db->sql_fetchrow($result)) 
		{
			$mess = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
			$mess->render();
			
			$submitValue = 'editRecord';
			$submitText = $lang['SaveButton'];
			
			if($row['l_image'] == '')
			{
				$image = '';
			}
			else
			{
				$image = '<img src="' . $row['l_image'] . '"/>';
			}
			$row['l_about'] = formatFormOutput($row['l_about']);
								
			include $skin_dir . 'admin/editlicense.tpl';
		}
		else
		{
			$mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			$mess->render();
		}
	}

	function renderAdd()
	{
		global $skin_dir,  $lang, $db;
		
		$mess = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$mess->render();
		
		$row['l_id'] = '';
		$row['l_name'] = '';
		$row['l_minRaces'] = '';
		$row['l_maxPoints'] = '';
		$row['l_pointDuration'] = '';
		$row['l_default'] = '';
		$row['l_order'] = '';
		$row['l_pointDuration'] = '';
		$row['l_about'] = '';
		$image = '';
					
		$submitValue = 'addRecord';
		$submitText = $lang['AddButton'];
		
		include $skin_dir . 'admin/editlicense.tpl';
	}
	
}
?>